<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: functorExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('functorExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">functorExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Illustrates the use of functors</p><dl class="section see"><dt>See also</dt><dd><a class="el" href="index.html#functors">Functors</a> in the ARIA overview</dd></dl>
<div class="fragment"><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  This is a class that has some callback methods. Functors which refer to these</span></div><div class="line"><span class="comment">  callbacks will be passed to the DriverClass.  </span></div><div class="line"><span class="comment">*/</span></div><div class="line"><span class="keyword">class </span>CallbackContainer</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"></div><div class="line">  <span class="keywordtype">void</span> callback1();</div><div class="line">  <span class="keywordtype">void</span> callback2(<span class="keywordtype">int</span> i);</div><div class="line">  <span class="keywordtype">bool</span> callback3(<span class="keyword">const</span> <span class="keywordtype">char</span> *str);</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> CallbackContainer::callback1()</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;CallbackContainer::callback1 called.\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> CallbackContainer::callback2(<span class="keywordtype">int</span> i)</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;CallbackContainer::callback2 called with argument of &#39;%d&#39;\n&quot;</span>, i);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> CallbackContainer::callback3(<span class="keyword">const</span> <span class="keywordtype">char</span> *str)</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;CallbackContainer::callback3 called with argument of &#39;%s&#39;.\n&quot;</span>, str);</div><div class="line">  <span class="keywordflow">return</span>(<span class="keyword">true</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/* </span></div><div class="line"><span class="comment"> * Functors can also invoke global functions.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> globalCallback()</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;globalCallback() called.\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  This is a &quot;driver&quot; class. It takes three functors of different types and</span></div><div class="line"><span class="comment">  will invoke the three functors. This is a typical use of</span></div><div class="line"><span class="comment">  functors: to pass information or event notifications between loosely</span></div><div class="line"><span class="comment">  coupled objects.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"><span class="keyword">class </span>DriverClass</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"></div><div class="line">  <span class="keywordtype">void</span> invokeFunctors();</div><div class="line"></div><div class="line">  <span class="keywordtype">void</span> setCallback1(<a name="_a0"></a><a class="code" href="classArFunctor.html">ArFunctor</a> *func) {myFunc1=func;}</div><div class="line">  <span class="keywordtype">void</span> setCallback2(<a name="_a1"></a><a class="code" href="classArFunctor1.html">ArFunctor1&lt;int&gt;</a> *func) {myFunc2=func;}</div><div class="line">  <span class="keywordtype">void</span> setCallback3(<a name="_a2"></a><a class="code" href="classArRetFunctor1.html">ArRetFunctor1&lt;bool, const char *&gt;</a> *func) {myFunc3=func;}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">protected</span>:</div><div class="line"></div><div class="line">  <a class="code" href="classArFunctor.html">ArFunctor</a> *myFunc1;</div><div class="line">  <a class="code" href="classArFunctor1.html">ArFunctor1&lt;int&gt;</a> *myFunc2;</div><div class="line">  <a class="code" href="classArRetFunctor1.html">ArRetFunctor1&lt;bool, const char *&gt;</a> *myFunc3;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> DriverClass::invokeFunctors()</div><div class="line">{</div><div class="line">  <span class="keywordtype">bool</span> ret;</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Invoking functor1... &quot;</span>);</div><div class="line">  myFunc1-&gt;invoke();</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Invoking functor2... &quot;</span>);</div><div class="line">  myFunc2-&gt;invoke(23);</div><div class="line">     </div><div class="line"></div><div class="line">  <span class="comment">/*</span></div><div class="line"><span class="comment">     For functors with return values, use invorkeR() instead of invoke()</span></div><div class="line"><span class="comment">     to get the return value.  The invoke() function can also be used to invoke</span></div><div class="line"><span class="comment">     the functor, but the return value is lost.  (And is a possible source</span></div><div class="line"><span class="comment">     of memory leaks if you were supposed to free a pointer returned.)</span></div><div class="line"><span class="comment">  */</span></div><div class="line">  printf(<span class="stringliteral">&quot;Invoking functor3... &quot;</span>);</div><div class="line">  ret=myFunc3-&gt;invokeR(<span class="stringliteral">&quot;This is a string argument&quot;</span>);</div><div class="line">  <span class="keywordflow">if</span> (ret)</div><div class="line">    printf(<span class="stringliteral">&quot;\t-&gt; functor3 returned &#39;true&#39;\n&quot;</span>);</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    printf(<span class="stringliteral">&quot;\t-&gt; functor3 returned &#39;false&#39;\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">  CallbackContainer cb;</div><div class="line">  DriverClass driver;</div><div class="line"></div><div class="line">  <a name="_a3"></a><a class="code" href="classArFunctorC.html">ArFunctorC&lt;CallbackContainer&gt;</a> functor1(cb, &amp;CallbackContainer::callback1);</div><div class="line">  <a name="_a4"></a><a class="code" href="classArFunctor1C.html">ArFunctor1C&lt;CallbackContainer, int&gt;</a> functor2(cb, &amp;CallbackContainer::callback2);</div><div class="line">  <a name="_a5"></a><a class="code" href="classArRetFunctor1C.html">ArRetFunctor1C&lt;bool, CallbackContainer, const char *&gt;</a></div><div class="line">    functor3(cb, &amp;CallbackContainer::callback3);</div><div class="line"></div><div class="line">  driver.setCallback1(&amp;functor1);</div><div class="line">  driver.setCallback2(&amp;functor2);</div><div class="line">  driver.setCallback3(&amp;functor3);</div><div class="line"></div><div class="line">  driver.invokeFunctors();</div><div class="line"></div><div class="line">  <span class="comment">/* You can make functors that target global functions too. */</span></div><div class="line">  <a name="_a6"></a><a class="code" href="classArGlobalFunctor.html">ArGlobalFunctor</a> globalFunctor(&amp;globalCallback);</div><div class="line">  printf(<span class="stringliteral">&quot;Invoking globalFunctor... &quot;</span>);</div><div class="line">  globalFunctor.<a name="a7"></a><a class="code" href="classArGlobalFunctor.html#aa6946d25fd07a25d976deeafbb53b576">invoke</a>();</div><div class="line"></div><div class="line">  <span class="comment">/* You can also include the values of arguments in an ArFunctor object, if you</span></div><div class="line"><span class="comment">   * want to use the same value in every invocation of the functor.</span></div><div class="line"><span class="comment">   */</span></div><div class="line">  <a class="code" href="classArFunctor1C.html">ArFunctor1C&lt;CallbackContainer, int&gt;</a> functor4(cb, &amp;CallbackContainer::callback2, 42);</div><div class="line">  printf(<span class="stringliteral">&quot;Invoking functor with constant argument... &quot;</span>);</div><div class="line">  functor4.<a name="a8"></a><a class="code" href="classArFunctor1C.html#ae2149b36ea519169f5040acf0fc59f60">invoke</a>();</div><div class="line"></div><div class="line">  <span class="comment">/* Functors can be downcast to parent interface classes, as long as their invocation</span></div><div class="line"><span class="comment">   * does not require arguments.</span></div><div class="line"><span class="comment">   */</span></div><div class="line">  <a class="code" href="classArFunctor.html">ArFunctor</a>* baseFunctor = &amp;functor4;</div><div class="line">  printf(<span class="stringliteral">&quot;Invoking downcast functor... &quot;</span>);</div><div class="line">  baseFunctor-&gt;<a name="a9"></a><a class="code" href="classArFunctor.html#a1c87b5ef305e653fe13258ebe77faee3">invoke</a>();</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keywordflow">return</span>(0);</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
